package com.itheima.d7_sync.no3;

import java.util.concurrent.locks.ReentrantLock;

//目标：掌握Lock锁给线程代码上锁
public class Demo03 {
    public static void main(String[] args) {
        /**
         * Lock是接口不能直接实例化，这里采用它的实现类ReentrantLock来构建Lock锁对象
         *      lock()：获得锁
         *      unlock()：释放锁
         * 注意：锁对象要唯一
         */
        Account account = new Account();
        account.setBalance(100000);

        //1.创建ReentrantLock锁对象（依然要保证锁对象是唯一的！）
        ReentrantLock lock = new ReentrantLock();

        //2.创建MyRunnable任务对象，传入账户、取的钱数、锁
        MyRunnable runnable = new MyRunnable(account, 100000, lock);

        //3.创建两个线程对象，传入任务
        Thread t1 = new Thread(runnable, "小明");
        Thread t2 = new Thread(runnable, "小红");

        //4.开启线程
        t1.start();
        t2.start();
    }
}
